$(function() {
	// 获取本地存储对象 
	var localStorage = window.localStorage;
	var confirmMID = localStorage.getItem("key");
	$.post("http://localhost:8081/study/permission/findAllPathByroleId", {
		confirmMID: confirmMID
	}, function(data) {
		console.log(data);
		if(data.success) {
			$("#contentTree").html(data.data);
		}
		$('.tree li:has(ul)').addClass('parent_li').find(' > span').attr('title', 'Collapse this branch');

		$('.tree li.parent_li > span').on('click', function(e) {

			var children = $(this).parent('li.parent_li').find(' > ul > li');

			if(children.is(":visible")) {

				children.hide('fast');

				$(this).attr('title', 'Expand this branch').find(' > i').addClass('icon-plus-sign').removeClass('icon-minus-sign');

			} else {

				children.show('fast');

				$(this).attr('title', 'Collapse this branch').find(' > i').addClass('icon-minus-sign').removeClass('icon-plus-sign');

			}

			e.stopPropagation();

		});
	})

    $("#submit").click(function(){

        var checkBoxArray = $(".choose");
        var roleName = $("#roleName").val();
        var remark = $("#remark").val();

        console.log(checkBoxArray.length)
        var permissionList = "";
        $.each(checkBoxArray, function(index,data) {
            var permission = $(data).val();
            console.log(permission);
            var ss = $(data).prop("checked");
            if(ss){
                permissionList +=permission+","
            }
        });
        console.log(permissionList);
        $.post("http://localhost:8081/study/permission/CreateRole",{
            confirmMID:confirmMID,
            roleName:roleName,
            remark:remark,
            permissionList:permissionList
        },function(data){
            console.log(data);
            if(data.success){
                alert(data.message)
                window.location.href="#"
            }else{
                alert(data.message)
            }
        })
    })

    //禁用form表单自定义action提交功能
    $(document).ready(function() {
        $("form").submit(function(e) {
            e.preventDefault();
            console.log("Submit prevented");
        });
    });
});

function chooseFather(a) {
    var checked = $(a).prop("checked");
    console.log(checked)
    if(checked) {
        $(a).parent().parent().prev().prop("checked",true);
    }

    var arrays = $(a).parent().parent().find("input")
    console.log(arrays.length)
    var flag = true;
    $.each(arrays, function(index, data) {
        console.log(data)
        var checked = $(data).prop("checked");
        console.log(checked);
        if(checked) {
            flag = false;
        }
    })
    console.log(flag)
    if(flag) {
        $(a).parent().parent().prev().prop("checked",false);
    }
};

function chooseAll(item) {
    var fatherChecked = $(item).prop("checked");
    if(!fatherChecked){
        $(item).prop("checked",false);
    }
    console.log(fatherChecked)
    var arrays = $(item).next().find("input");
    console.log(arrays.length + "::" + fatherChecked)
    $.each(arrays, function(index, data) {
        console.log(data)
        if(fatherChecked) {
            $(data).prop("checked",true);
        } else {
            $(data).prop("checked",false);
        }
    })
